package project.daggerdemo.base;

import android.app.Application;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.compat.BuildConfig;
import android.util.Log;

import project.daggerdemo.base.di.components.AppComponent;
import project.daggerdemo.base.di.components.DaggerAppComponent;
import project.daggerdemo.base.di.modules.AppModule;
import project.daggerdemo.network.response.GitHubApiModule;
import timber.log.Timber;

/**
 * Created by lgs on 2018/3/3.
 */

public class BaseApp extends Application {

    private AppComponent appComponent;

    public static BaseApp get(Context context) {
        return (BaseApp) context.getApplicationContext();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //在这里先使用Timber.plant注册一个Tree，然后调用静态的.d .v 去使用
        if (BuildConfig.DEBUG) {
            Timber.plant(new Timber.DebugTree());
//            AndroidDevMetrics.initWith(this);
        } else {
            Timber.plant(new CrashReportingTree());
        }
        initAppComponent();
    }

    private void initAppComponent() {
        appComponent = DaggerAppComponent.builder()
                .appModule(new AppModule())
                .gitHubApiModule(new GitHubApiModule())
                .build();
    }

    /**
     * A tree which logs important information for crash reporting.
     */
    private static class CrashReportingTree extends Timber.Tree {
        @Override
        protected void log(int priority, String tag, @NonNull String message, Throwable t) {
            if (priority == Log.VERBOSE || priority == Log.DEBUG) {
                return;
            }

          /*  FakeCrashLibrary.log(priority, tag, message);

            if (t != null) {
                if (priority == Log.ERROR) {
                    FakeCrashLibrary.logError(t);
                } else if (priority == Log.WARN) {
                    FakeCrashLibrary.logWarning(t);
                }
            }*/
        }
    }
}
